
/**
 * author:
 * 功能：
 * 一。保存报警类型，用于本地显示：
 * 增加代码：
 * 1.定义alarmTypeData，用于主页面定义本对象后存放类型数据
 * 2.弹窗显示时通过方法获取文字描述信息
 * 二。主动防控报警可选
 * 1.修改原本的通过datagrid的load加载为ajax加载，并对数据进行处理
 */


AlarmGrid = {};

//以下为修改代码增加变量
AlarmGrid.num=0;//定义计数器，以统计接收报警数据次数/////////////////////////修改代码
AlarmGrid.alarmTypeData = {};//定义保存报警类型的数据
AlarmGrid.activeFlg = false;//主动安全，默认关闭
AlarmGrid.params = {};//显示定义参数对象
AlarmGrid.activeSafeName = "主动安全报警";//定义主动安全报警文字对象，以便统一修改，备注，暂时未确定全部统一，有可能有未统一的
//以上为修改代码增加变量


AlarmGrid.sound=true;//报警声音默认打开
AlarmGrid.popup = true;//弹窗默认打开
AlarmGrid.refreshInterval = 5;
AlarmGrid.displayTime = null;
AlarmGrid.autoRefresh = false;
AlarmGrid.popNum = 0;
AlarmGrid.showVehicleOnMap = false;//报警的时候，显示车辆在地图中心
AlarmGrid.maxPopupNum = 20;//最多弹窗的报警条数，超过后，前面的要清除避免占用内存
AlarmGrid.videoWithAlarm = false;//视频报警联动功能是否具备
AlarmGrid.create = function(alarmInterval,showVehicle)
{
	if(alarmInterval && alarmInterval > 5)
	    this.refreshInterval = alarmInterval;
	if(showVehicle)
		this.showVehicleOnMap = showVehicle;

    $('#alarmWindow').window('close');//初始化时，隐藏报警弹屏窗口
	this.alarmDataGrid=	$("#alarmDataGrid");	
	// var url = globalConfig.webPath+"/alarm/paginate.action";
    this.alarmDataGrid.datagrid({
                columns: [[
                    { title: '车牌号', field: 'plateNo', width: 80,minWidth:80 },
					{ title: '报警类型', field: 'alarmTypeName', width: 140 },
                    { title: '报警来源', field: 'alarmSourceName', width: 120,minWidth:80 },
					{ title: '报警时间', field: 'startTime', width: 130,minWidth:100 },
					{ title: '报警地点', field: 'location', width: 280,minWidth:300 },
					{ title: '处理状态', field: 'processed', width: 80,minWidth:80 },
					{ title: '所属车组', field: 'depName', width: 110,minWidth:80 }
					//下面三个字段暂不需要
                    //{ title: '颜色', field: 'plateColor', width: 45,minWidth:60 },
                    // { title: 'Sim卡号', field: 'simNo', width: 105,minWidth:120 },
                    // { title: 'id', field: 'id', width: 0,minWidth:0 }
                    ]],
                //height: 165,
				//url: url,
				method: 'POST',
				fit:true,
			    toolbar:"#alarmGridToolbar",
				//queryParams: { 'queryId': 'alarm.selectProcessedAlarms',startTime: Utility.today() },
				idField: 'id',
				striped: true,
				loadMsg:'',
				fitColumns: true,
				singleSelect: true,
				rownumbers: true,
				pagination: true,
				nowrap: true,
				pageSize: 10,
				onDblClickRow:this.onDblClickRow,//表格的双击事件定义
				pageList: [10, 20, 50, 100, 150, 200],
				showFooter: true,
				// onLoadSuccess:this.onLoadSuccess//表格加载成功事件
            });
	 //this.refreshInterval = 10;
	 this.timerName = "alarmTimer";
	 var strInterval = this.refreshInterval + 's';
	//  this.params = {queryId:"alarm.selectProcessedAlarms",startTime: Utility.today()};
	 var me = this;
	 /**
	 $('body').everyTime(strInterval, this.timerName,function(){
		me.refreshNewAlarm();
	 });
	 */
	// var basicDataUrl = globalConfig.webPath + "/data/basicData.action";

/**
 * 原有代码，注释掉，
 * 
 * 
 * // var comboboxId = "alarmType";
	//  Utility.easyCombobox(comboboxId,{queryID:"alarm.selectAlarmTypeList"});
	//  comboboxId = "alarmSrc";
	//  Utility.easyCombobox(comboboxId,{category:"AlarmSource"});
 * 
 * 
 */

	//代码修改区
	///
	//////
	
	//获取数据并填充
	me.getTypeData();
	$(document).oneTime(500,"getPageData",function(){
		me.getAlarmPageData();
	});

	//屏蔽主动安全,如果屏蔽，则不填充主动安全内容
	$("#disableAlarmActiveSafe").click(function()
	{
		 var checked = $(this).attr("checked")=="checked";
		 me.activeFlg = checked;
		//  console.log(checked);
		//  console.log(me.activeFlg);
		 if(!checked)
		{
			//屏蔽主动安全
			 me.fillAlarmSouce("主动安全报警");
			 me.fillAlarmType("请选择");
		}else{
			//接触屏蔽
			me.fillAlarmSouce("");
			me.fillAlarmType("请选择");
		}
		me.getAlarmPageData();
	});

	//查询事件定义
	$("#btnQueryAlarmData").click(function(){
		
		// console.log(me.params);
		me.setFirstPage();
		me.getAlarmPageData();
		
		// me.alarmDataGrid.datagrid('load',me.params);
	});

	//选择页面事件定义
	$("#alarmDataGrid").datagrid("getPager").pagination({
		onSelectPage:function(pageNumber,pageSize){
			//设置新的页面信息
			me.params.pageNumber = $("#alarmDataGrid").datagrid("getPager").data("pagination").options.pageNumber;
			me.params.pageSize = $("#alarmDataGrid").datagrid("getPager").data("pagination").options.pageSize;
			//查询数据
			me.getAlarmPageData();
		}
	});
	
	//////
	 /////代码修改区

	 $("#muteAlarmSound").click(function()
		{
		     var checked = $(this).attr("checked")=="checked";
			 AlarmGrid.sound = checked != true;
			 AlarmSound.enable(AlarmGrid.sound);
		});
	 $("#disableAlarmWindow").click(function()
		{
		     var checked = $(this).attr("checked")=="checked";
			 AlarmGrid.popup = checked != true;
		});
	 $("#autoRefreshAlarmGrid").click(function()
		{
		 try {
			 var checked = $(this).attr("checked") == "checked";
			 me.autoRefresh = checked;
			 if (checked) {
				 // me.getParams();
				 //  me.alarmDataGrid.datagrid('load',me.params);
				 //重写加载数据方法
				 $(document).everyTime("25s", "refreshAlarmData", function () {
					 console.log("刷新报警数据...");
					 me.setFirstPage();
					 me.getAlarmPageData();
				 });

			 }
		 } catch (error) {
			 console.error("设置定时刷新报警数据错误：" + error);
		 }
		     
		});

	
/*
	$("#btnQueryAlarmData").click(function()
		{
		    var plateNo = $("#alarmPlateNo").val();
		    var alarmType = $('#alarmType').combobox('getValue');
		    var alarmSrc = $("#alarmSrc").combobox('getValue');
			me.params = {plateNo:plateNo, alarmType:alarmType,alarmSource:alarmSrc,queryId:"alarm.selectProcessedAlarms"};
			console.log(me.params);
		    me.alarmDataGrid.datagrid('load',me.params);
		});
		*/

	$("#btnResetAlarm").click(function()
		{
		    // $("#alarmPlateNo").val('');
		    // $('#alarmType').combobox('setValue','');
		    // $("#alarmSrc").combobox('setValue','');
			// me.params = {queryId:"selectProcessedAlarms"};
			//以上代码屏蔽不需要
			me.getAlarmPageData();
		});

	try
	{
        this.startWebSocket();
	}catch (e)
	{
		console.log(e);
	}

	return alarmDataGrid;
}

var sockjsClient = null;
AlarmGrid.startWebSocket = function()
{
               var me = this;
        		var sockjsAddr = globalConfig.webPath + "/ws/alarm";
        		
        		var onopen = function() {
        			console.log('open');
        			//$("#status").val("已连接");
					try {
						var isSendSuccess = sockjsClient.send("hello");
						if (!isSendSuccess) {
							console.log("发送失败");
						} else
						{
							console.log("成功");
						}
					} catch (e) {
							console.log("发送失败");
					}
        		 };
        		 var onmessage = function(e) {
					 try
					 {						 
        			    me.newAlarm(e.data);
						if(me.autoRefresh){
							// me.getParams();
							// me.alarmDataGrid.datagrid('load',me.params);
							me.getAlarmPageData();
						}
				           
        			    //console.log(e.data);
					 }
					 catch (e)
					 {
        				console.log('alarm onmessage error:' + e.message);
					 }
        		 };
        		 var onclose = function() {
        			 console.log('close');
					 me.startWebSocket();
        			 //$("#status").val("未连接");
        		 };
        		 
				var id = "1";
        		sockjsClient = new SockJS(sockjsAddr + "/" + id);
        		sockjsClient.onopen = onopen;
        		sockjsClient.onmessage = onmessage;
        		sockjsClient.onclose = onclose;       		
        		
        		

}

/**
 * 
 * 代码修改区
 * author：shinion
 * date：2018-12-14
 * 功能：
 * 1.修改combox显示和无法级联问题
 * 2.修改无法屏蔽主动安全报警信息
 * 3.如果屏蔽主动安全报警信息，那么弹出窗口也需要屏蔽主动安全报警信息
 * 
 * 
 */

 //获取报警类型数据，并把相应类型放入报警来源下拉框
AlarmGrid.getTypeData = function(){
	//报警表格所需
	//获取报警类型基本数据，并保存在本地
	var me = this;
	try {
		//获取报警数据
		$.ajax({
			type: "get",//请求方式
			url: globalConfig.webPath+"/activeSafeAlarm/getAlarmSources.action",
			data: null,
			contentType: "application/json;charset=utf-8",
			success: function (data) {
				//获取数值，赋值
				me.alarmTypeData = data;
				// console.log("获取数据成功：");
				// console.log(me.alarmTypeData);
				if(me.activeFlg){
					me.fillAlarmSouce("");//全部填充
				}else{
					me.fillAlarmSouce("主动安全报警");//全部填充
				}
				
			}
		});
	} catch (error) {
		console.error("报警功能中获取报警基本类型数据出错：" + error);
	}
}

//1.填充报警类型来源信息，并根据报警来源的选择，填充报警类型
//如果参数不为空，则和参数名一样的参数不绑定，为屏蔽主动安全做准备
AlarmGrid.fillAlarmSouce = function(alarmSourceName) {
	try {
		var me = this;
		if (this.alarmTypeData != null) {
			var alarmTypeDataTemp = this.alarmTypeData;
			var alarmSouceArrayTemp = new Array();
			var arrayTemp = new Array;//临时数组，用于临时存放报警来源文字信息
			//填充第一条
			var objTemp = { text: "请选择", value: 0 };
			arrayTemp.push("请选择");
			alarmSouceArrayTemp.push(objTemp);
			if (alarmTypeDataTemp.length > 0) {
				for (var i = 0; i < alarmTypeDataTemp.length; i++) {
					var asTemp = alarmTypeDataTemp[i].alarmSourceName;
					if (arrayTemp.indexOf(asTemp) <= -1 && asTemp!=alarmSourceName) {
						//如果临时数组中不包含该条报警来源，并且不是屏蔽项则加入
						arrayTemp.push(asTemp);
						alarmSouceArrayTemp.push({ text: asTemp, value: (i + 1) });
					}
				}//end for
				arrayTemp = [];
			}
			// console.log("报警来源数据");
			// console.log(alarmSouceArrayTemp);
			//绑定数据
			if (alarmSouceArrayTemp.length > 0) {
				$("#alarmSrc").combobox({
					textField: "text",
					valueField: "value",
					data: alarmSouceArrayTemp,
					select: alarmSouceArrayTemp[0].value,
					onSelect: function (record) {//选择事件
						// console.log(record.text);
						// console.log("选择事件");
						me.fillAlarmType(record.text);
					}
				});
			}
		}//end alarmTypeData
	} catch (error) {
		console.error("填充报警来源信息出错" + error);
	}
	
}

/*
1.格式化数据，根据传值过来的文字，选择报警来源文字相同的报警类型文字，存入数据数组
2.初始化报警类型下拉框，并填充数据
*/
AlarmGrid.fillAlarmType = function(text){
	try {
		
		if (text != "" && this.alarmTypeData != null) {
			var alarmTypeDataTemp = this.alarmTypeData;
			var alarmTypeArrayTemp = new Array();//数据数组
			//填充第一条
			var objTemp = { text: "请选择", value: 0 };
			alarmTypeArrayTemp.push(objTemp);
			//开始填充数据
			for (var i = 0; i < alarmTypeDataTemp.length; i++) {
				var atTemp = alarmTypeDataTemp[i].alarmSourceName;
				//如果当前报警信息的文字和需要显示的相同
				if (atTemp == text) {
					alarmTypeArrayTemp.push({ text: alarmTypeDataTemp[i].alarmTypeName, value: (i + 1) });
				}
			}

			// console.log("报警类型数据");
			// console.log(alarmTypeArrayTemp);


			//初始化下拉框控件并绑定数据
			if (alarmTypeArrayTemp != null) {
				$("#alarmType").combobox({
					textField: "text",
					valueField: "value",
					data: alarmTypeArrayTemp,
					select: alarmTypeArrayTemp[0].value
					//选择事件不添加，暂不需要
				});
			}
		}

	} catch (error) {
		console.error("填充报警类型出错" + error);

	}
}


/**
 * 
 * 从服务器获取数据
 * 
 * 从服务器获取数据
 * 
 */
AlarmGrid.getAlarmPageData =function(){
	// console.log("加载");
	var me = this;
	try {
		me.getParams();
		var jsonData = JSON.stringify(me.params);
		// console.log(jsonData);
		$("#alarmDataGrid").datagrid("getPager").pagination("loading");
		$.ajax({
			type: "post",
			url: globalConfig.webPath+"/activeSafeAlarm/getAlarmPageData.action",//请求地址
			dataType: "json",
			data: jsonData,
			contentType: "application/json;charset=utf-8",
			success: function (data) {
				me.addData(data.rows);
				$("#alarmDataGrid").datagrid('loadData', data);//绑定数据
				$("#alarmDataGrid").datagrid("getPager").pagination("loaded");//刷新动画停止

				console.log("接收到查询报警数据->");
				console.log(data);
			}
		});
	} catch (error) {
		console.error("获取报警数据错误:" + error);
	}
}




//设置页面为显示第一页
AlarmGrid.setFirstPage = function() {
	var me = this;
	var opts = $("#alarmDataGrid").datagrid("options");
	var pager = $("#alarmDataGrid").datagrid("getPager");
	// console.log(opts);
	// console.log(pager);
	opts.pageNumber = 1;
	opts.pageSize=me.params.pageSize;
	pager.pagination("refresh", {
		pageNumber: 1,
		pageSize: opts.pageSize
	});
}


/*
*处理数据，加上报警来源和报警类型
*/
AlarmGrid.addData = function(data){
	if (data != null) {//如果数据为空，那么不处理
		var me = this;
		var alarmTypeData = this.alarmTypeData;
		try {
			if (alarmTypeData != null) {
				//开始处理数据
				for (var i = 0; i < data.length; i++) {
					var temp = me.getOneTypeData(data[i].alarmSource, data[i].alarmType);
					data[i].alarmSourceName = temp.alarmSourceName;
					data[i].alarmTypeName = temp.alarmTypeName;
					if (data[i].processed == 0) {
						var processedTemp = parseInt(data[i].processed)
						// console.log(me.getProcessNamed(processedTemp));
						data[i].processed = me.getProcessNamed(data[i].processed);

					}
				}
			}
		} catch (error) {
			console.error("处理数据错误" + error);
		}
	}
	
}

//根据值，获取数据
AlarmGrid.getOneTypeData = function(alarmSource,alarmType){
	try {
		var alarmTypeData = this.alarmTypeData;
		if (alarmTypeData != null) {
			for (var i = 0; i < alarmTypeData.length; i++) {
				if (alarmTypeData[i].alarmSource == alarmSource && alarmTypeData[i].alarmType == alarmType)
					return alarmTypeData[i];
			}
		} else {
			return null;
		}
	} catch (error) {
		console.error("获取类型描述错误:" + error);
	}
	
}

//根据处理数据获取处理类型
AlarmGrid.getProcessNamed =function(id){
	switch(id){
		case 0:
		return "未处理";
		case 1:
		return "监听";
		case 2:
		return "拍照";
		case 3:
		return "文本下发";
		case 4:
		return "报警解除";
		default:
		return "已处理";
	}
}


//获取系统参数
AlarmGrid.getParams = function(){
	var me = this;
	me.params={};
	me.params.queryId = "alarm.selectProcessedAlarms";
	me.params.startTime = Utility.today();

	me.params.plateNo = $("#alarmPlateNo").val();

	me.params.pageNumber = $("#alarmDataGrid").datagrid("getPager").data("pagination").options.pageNumber;
	me.params.pageSize = $("#alarmDataGrid").datagrid("getPager").data("pagination").options.pageSize;


	me.getAlarmParams();
}

//获取类型和来源参数信息
/**
 * 获取类型和来源参数信息
 * 1.首先根据来源获取报警类型数据，如果获取到数据，说明选择了报警来源，并且如果屏蔽了主动安全，肯定选不到主动安全，
 * 		那么就可以设置报警来源参数alarmSource，然后从获取的数据中去获取报警类型，
 * 		如果获取不到，那么就不设参数，全选，如果获取到了，就设置alarmType
 * 2.如果获取不到报警来源数据，那么说明没有选择或者没有正确选择，那么如果没有屏蔽主动安全报警，则不用设置参数
 * 		如果设置了主动安全报警，那么去获取除主动安全以外的所有数据信息,放入alarmSourceList
 */
AlarmGrid.getAlarmParams = function(){
	var me = this;
	var alarmSourceText = $("#alarmSrc").combobox("getText");
	var alarmTypeText = $("#alarmType").combobox("getText");
	// console.log(alarmSourceText);
	// console.log(alarmTypeText);
	try {
		var alarmSourceDataList = me.getAlarmData(alarmSourceText);
		// console.log("报警来源列表：");
		// console.log(alarmSourceDataList);
		if(alarmSourceDataList.length >0){
			//有数据，说明选择了报警来源，获取列表中所有不同的报警来源的值
			var alSourceValueList = me.getAlarmSources(alarmSourceDataList);
			// console.log("报警来源值：");
			// console.log(alSourceValueList);
			if(alSourceValueList.length>1){
				me.params.alarmSourceList = alSourceValueList;
			}else if(alSourceValueList.length == 1){
				me.params.alarmSource = alSourceValueList[0];
			}
			//获取报警类型的值
			var alarmTypeTemp = me.getAlarmTypeData(alarmSourceDataList,alarmTypeText);
			if(alarmTypeTemp != ""){
				me.params.alarmType = alarmTypeTemp;
			}
		}else{
			//没有数据，标明没有选择
			//如果没有屏蔽主动安全，则全查，不用设置参数，如果屏蔽主动安全，则需要设置报警来源列表，放入所有非主动安全报警
			if(!me.activeFlg){
				//主动安全屏蔽，不显示，则查询参数不包括
				me.params.alarmSourceList = me.getNotActiveList();
			}
		}
	} catch (error) {
		console.log("获取报警类型和报警来源参数错误："+error);
	}
}

//获取所有非主动安全的报警来源

AlarmGrid.getNotActiveList = function(){
	var alarmDataList = new Array();
	try {
		if(this.alarmTypeData != null)
		{
			var alarmTemp = this.alarmTypeData;
			
			for(var i=0 ; i<alarmTemp.length ; i++){
				//循环比对，如果有正确的，则放入队列
				if(alarmDataList.indexOf(alarmTemp[i].alarmSource)<=-1 && alarmTemp[i].alarmSourceName!="主动安全报警")
				{
					alarmDataList.push(alarmTemp[i].alarmSource);
				}
			}
			// console.log(alarmDataList);
			return alarmDataList;
		}
	} catch (error) {
		console.log("根据报警类型来源文字获取数据出错："+error);
	}
	return alarmDataList;
}

//从报警来源数组中获取所有的值
AlarmGrid.getAlarmSources = function(alarmSourceList){
	var alSourceValueList = new Array();
	try {
		if(alarmSourceList.length > 0){
			for(var i=0 ; i<alarmSourceList.length ; i++){
				if(alSourceValueList.indexOf(alarmSourceList[i].alarmSource)<=-1){
					alSourceValueList.push(alarmSourceList[i].alarmSource);
				}
			}
		}
	} catch (error) {
		console.error("获取报警来源数值错误："+error)
	}

	return alSourceValueList;
}

//根据报警类型名称从指定列表中获取值
AlarmGrid.getAlarmTypeData = function(alarmSourceList,alarmTypeName){
	var alarmDataTemp = "";
	try {
		if(alarmSourceList.length > 0){
			for(var i=0 ; i<alarmSourceList.length ; i++){
				if(alarmSourceList[i].alarmTypeName == alarmTypeName)
				{
					//如果链表中有该选项，那么把该数据返回,因为正常数据就一条
					alarmDataTemp = alarmSourceList[i].alarmType;
					return alarmDataTemp;
				}
			}
		}
	} catch (error) {
		console.error("根据类型名称从来源表中获取数据错误"+error)
	}
	return alarmDataTemp;
}

//根据报警来源获取数据
AlarmGrid.getAlarmData = function(alarmSourceName){
	var alarmDataList = new Array();
	try {
		if(this.alarmTypeData != null)
		{
			var alarmTemp = this.alarmTypeData;
			
			for(var i=0 ; i<alarmTemp.length ; i++){
				//循环比对，如果有正确的，则放入队列
				if(alarmTemp[i].alarmSourceName == alarmSourceName)
				{
					alarmDataList.push(alarmTemp[i]);
				}
			}
			return alarmDataList;
		}
	} catch (error) {
		console.error("根据报警类型来源文字获取数据出错："+error);
	}
	return alarmDataList;
}



/**
 * 
 * 根据报警信息确定是否为主动安全信息，如果是，并且不弹出，则返回false，如果允许弹出，则返回true
 */
AlarmGrid.getPupFlg = function(alarm){
	var returnFlg = true;//默认全部弹出
	var myActiveFlg = this.activeFlg;
	var myAlarmTypeTemp = this.alarmTypeData;
	if(myActiveFlg){
		//如果为true，则代表可以弹出主动安全报警，直接返回true即可
		return returnFlg;
	}else{
		//如果不弹出，则查看该报警信息是否为主动安全报警，如果是，则返回false，如果不是，则返回true
		if(myAlarmTypeTemp != null)
		{
			for(var i=0 ; i<myAlarmTypeTemp.length ; i++){
				if(myAlarmTypeTemp[i].alarmSource == alarm.alarmSource){
					if(myAlarmTypeTemp[i].alarmSourceName=="主动安全报警")
					{	//同时满足报警来源值相同并且来源描述为“主动安全报警”，才阻止弹出，其余不弹出
						returnFlg = false;
						return returnFlg;
					}
				}
			}
		}
	}
	return returnFlg;
}	


/**
 *新增加事件方法： 
 * 数据加载成功方法
 * 该功能暂不需要，注释
 * author：shinion
 * date：2018-12-15
 */
/*
AlarmGrid.onLoadSuccess = function(){
	try {
		console.log("load success");
		this.alarmDataGrid = $("#alarmDataGrid");
		console.log(this.alarmDataGrid.datagrid("getData"));
	} catch (error) {
		console.error("加载完成后解析出错："+error);
	}
	
}*/



/**
 * author:shinion
 * data:2018-12-17 09:00:00
 * 主动安全报警暂未获取到有效索引数据，功能暂无法实现
 */
showSafeWindow = function(alarmId){
	try {
		console.log("暂时无法显示主动安全报警信息");
		console.log(alarmId);
	} catch (error) {
		console.error("弹出报警点击报警数据，显示主动安全报警列表查看错误：" + error);
	}
	
}







/**
 * 
 * 
 * 代码修改区结束
 * author：shinion
 * date：2018-12-14
 * 
 */









//新数据处理方法
AlarmGrid.newAlarm = function(strAlarmData)
{
	
	this.num++;
	var nowTime = new Date;
	console.log("第" +this.num+"次报警数据接收，时间："+nowTime.toLocaleString());	
	// console.log(strAlarmData);
	var alarmList = JSON.parse(strAlarmData);
	var me = this;
	
	if(me.popNum > me.maxPopupNum)
					{
						me.closeWindow();//累计条数超过限制，清空避免占用内存
					}
	var isPop = false;
	var lastAlarm = null;
	$.each(alarmList, function(i,alarm)
	{
		if(alarm.popupEnabled)
		{
			var pupFlg = me.getPupFlg(alarm);
			//如果允许该条报警弹出，则弹出，否则该条报警信息不处理/////////////修改代码，author：shinion，原代码没有屏蔽主动安全报警条件
			if (pupFlg) {
				me.updateNotify(alarm);
				isPop = true;
				lastAlarm = alarm;
			}
			 
		}
		me.displayAlarm(alarm);
		
	});
	if(isPop)
	{
		this.popNotifyWindow();//弹出窗口
	    if(lastAlarm.soundEnabled)
				AlarmSound.play();//报警声音打开
	}
}


//在地图上显示报警信息
AlarmGrid.displayAlarm = function(alarm)
{
	
	if(AlarmGrid.showVehicleOnMap == true && alarm.alarmType != "GpsOnline" && alarm.alarmType != "GpsOffline" && alarm.messageType == "alarmNotify")
	{
		 MyMap.showVehicleAlarmOnMap(alarm.vehicleId,alarm.alarmDescr); //调用main.jsp的jS方法,显示车辆在地图中心
	}

	if(alarm.videoChannels && this.popup == true && this.videoWithAlarm == true)
	{
        //$('#mainTab').tabs('select',"实时视频");
		var channelArray = alarm.videoChannels.split(",");
		var titles = [];
		$.each(channelArray, function(i,v)
		{
			titles.push(alarm.plateNo + " 通道" + v + "," + alarm.alarmDescr);
		});
		var alarmObj = {type:alarm.alarmType,name:alarm.alarmDescr,alarmPackNumber:alarm.alarmId};
		//MyVideoPlayer.previewVideo(alarm.vehicleId, alarm.simNo, channelArray, titles,"60", alarmObj );
        //InfoWindow.viewRealTimeVideo(alarm.vehicleId);
		var alarmWindowTitle = alarm.plateNo + alarm.alarmDescr;
        AlarmVideoWindow.viewRealTimeVideo(alarm.vehicleId, alarm.plateNo, alarm.simNo,alarmWindowTitle, channelArray);
	}

	if(alarm.messageType == "809Notify")
	{
		var strType = alarm.alarmType;
		if(strType == "0x9301") //查岗请求，弹出查岗的窗口
		{
			var url = globalConfig.webPath+"/platformPostQuery/view.action?commandId="+alarm.alarmId;
			InfoWindow.open(url, 630,350,"上级平台查岗");
		}else if(strType == "0x9302") //平台间报文请求，弹出平台间报文信息的窗口
		{
			var url = globalConfig.webPath+"/platformText/view.action?commandId="+alarm.alarmId;
			InfoWindow.open(url, 630,350,"平台间报文");
		}else if(strType.indexOf("报警督办") >= 0)
		{
            var url = globalConfig.webPath+"/jt809AlarmTodo/view.action?msgId="+alarm.alarmId;
			InfoWindow.open(url, 500,540,"上级平台报警督办消息");
		}
	}else if(alarm.messageType == "Jt809CommandGrid")
	{
		//更新809政府平台的主连接和从连接的状态
				var ps = alarm.alarmDescr.split(",");
				var mainLinkState = ps[0];
				var subLinkState = ps[1];
				
				var src = globalConfig.webPath + "/image/no.png";
				if(mainLinkState == "已建立连接" || mainLinkState == "连接成功")
				{
					src =  globalConfig.webPath + "/image/ok.png";
				}
				$("#mainLinkState").attr("src",src);
                src = globalConfig.webPath + "/image/no.png";
				if(subLinkState == "已建立连接" || subLinkState == "连接成功")
				{
					src =  globalConfig.webPath + "/image/ok.png";
				}
				$("#subLinkState").attr("src",src);
	}
}

//双击表格，弹出报警处理窗口，提请用户处理报警，并根据类型弹出不同的报警处理页面
AlarmGrid.onDblClickRow = function(rowIndex, rowData)
{
	var me = this;
	console.log("双击报警数据行："+rowIndex);
	console.log(rowData);
	if(rowData != null){
		if(rowData.alarmSourceName == "主动安全报警"){
			//弹出主动安全报警列表信息
			var alarmIndex = rowData.alarmIndex;
			var plateNo = rowData.plateNo;
			var param = "?alarmIndex=" + alarmIndex+"&id="+rowData.id;
			var url = globalConfig.webPath+"/activeSafeAlarm/viewAdasAlarmList.action" + param;
			var title = plateNo + "主动报警信息列表-查看";
			InfoWindow.open(url, 1210, 700, title);
		} else {
			//弹出普通报警
			var alarmId = rowData.id;
			InfoWindow.viewAlarm(alarmId);
		}
	}
	//var url =globalConfig.webPath  + "/alarm/view.action?alarmId="+ alarmId;
	//InfoWindow.open(url, 400, 340, "报警处理");
}

/**
 * 刷新报警信息，重新获取
 *  
 */	
AlarmGrid.refreshNewAlarm = function()
{
	var url = globalConfig.webPath+"/alarm/refresh.action";
	var me = this;
	var params = {};
	 $.ajax({
            type: "POST",
            url: url,
			data:params,
			error:function(){
			   
			},
            success: function(data){
			   var result =  data;
			   if(result.success)
			   {
				   		
					if(me.popNum > me.maxPopupNum)
					{
						me.closeWindow();//累计条数超过限制，清空避免占用内存
					}

				   //me.addNewAlarm(result.data);
				   //延时关闭窗口
				   if(me.displayTime)
				   {
						var now = new Date();
						var interval = now - me.displayTime;
						interval = 0.001 * interval;
						if(interval > 10)
					   {
							me.closeWindow();
					   }
				   }

			   }else
			   {
				   window.location = globalConfig.webPath+"/logout.action";
			   }
            }
        });
}

/**
var tpl = new Ext.Template(//定义模板  
    '<table  border=0 cellpadding=10 cellspacing = 10>',  
    '<tr><td>车牌号:</td><td>{plateNo}</td><td >报警类型:</td><td style="color:red;">{alarmType}</td></tr>',  
    '<tr><td>行驶速度:</td><td>{velocity}Km/h</td><td>报警时间:</td><td>{startTime}</td></tr>',  
    //'<tr><td></td><td>{2}</td></tr>',  
    '</table>'  
) ; */
//var tpl = new Ext.Template( '[{startTime}]:[{plateNo}]   <span class="alarm_{alarmType}"> {alarmTypeDescr}</span>') ;


/**
 * 在弹出窗口显示报警信息
 */
var notifyWindow;
AlarmGrid.updateNotify = function(alarm)
{
	var me = this;
	//添加alarmSourceName字段数据，为添加报警提示准备数据，并且为点击报警提示做准备
	var alarmSourceName="";
	try {
		console.log(alarm);
		alarmSourceName = this.getAlarmSourceName(alarm.alarmSource);
		if (alarmSourceName != "") {
			alarm.alarmSourceName = alarmSourceName;//报警表添加字段alarmSourceName，为判断是否为主动报警提供标识
			alarmSourceName = '[' + alarmSourceName + ']';
		}
		// console.log( alarmSourceName);
	} catch (error) {
		console.error("报警数据转换来源出错:"+error);
	}
	//弹出条数加一
	this.popNum += 1;
	
	//区分来源进行不同情况区分，主动安全和非主动安全
	var alarmContent = alarm.alarmTime+':  ['+alarm.plateNo +']<span class="'+alarm.alarmType+'" style=\'color:blue\'>'+alarm.alarmDescr + '</span>'+alarmSourceName;
	//非报警类型不可点击
	if(alarm.alarmType != "GpsOnline" && alarm.alarmType != "GpsOffline" && alarm.messageType == "alarmNotify"){
		if(alarm.alarmSourceName != me.activeSafeName){
			//非主动安全报警
			alarmContent = '<a href="javascript:InfoWindow.viewAlarm(' + alarm.alarmId + ')">' + alarmContent + '</a>';
		}else{
			//主动安全报警
			alarmContent = '<a href="javascript:showSafeWindow(' + alarm.alarmId + ')">' + alarmContent + '</a>';
		}
	}
	//补全标签并显示到控件第一条
	alarmContent += "<br/>";
	$("#alarmBox").prepend(alarmContent);



	//原有代码，注释暂不使用，进行备份，不删除
	//  var alarmContent = alarm.alarmTime+':  ['+alarm.plateNo +']<span class="'+alarm.alarmType+'" style=\'color:blue\'>'+alarm.alarmDescr + '</span>'+alarmSourceName;
	//  if(alarm.alarmType != "GpsOnline" && alarm.alarmType != "GpsOffline" && alarm.messageType == "alarmNotify")
	//    alarmContent = '<a href="javascript:InfoWindow.viewAlarm(' + alarm.alarmId + ')">' + alarmContent + '</a>';
	//  alarmContent += "<br/>";
	//  $("#alarmBox").prepend(alarmContent);
}


//根据报警来源获取名称
AlarmGrid.getAlarmSourceName = function(alarmSource){
	try {
		var me = this;
		if (me.alarmTypeData != null) {
			for (var i = 0; i < me.alarmTypeData.length; i++) {
				if (me.alarmTypeData[i].alarmSource == alarmSource) {
					return me.alarmTypeData[i].alarmSourceName;
				}
			}
			return "";
		}
	} catch (error) {
		console.error("报警来源获取出错："+error);
		return"";
	}
	
}

//关闭弹窗
AlarmGrid.closeWindow = function()
{
	this.popNum = 0;
    $('#alarmWindow').window('close');
	this.clearWindow();
}

//弹出窗口
AlarmGrid.popNotifyWindow = function(alarm)
{
	//如果禁止弹窗，则直接返回，不再打开窗口
	if(AlarmGrid.popup == false) 
		return;
	if(notifyWindow == null)
	{
		notifyWindow = $('#alarmWindow').window({
			title: '车辆报警消息',
			width: 320,
			height: 200,
			top: ($(window).height() - 200),
			left: ($(window).width() - 320),
			shadow: false,
			modal: false,
			iconCls: 'icon-alarm',
			closed: true,
			minimizable: false,
			maximizable: false,
			collapsible: true
		});
	}

    $('#alarmWindow').window('open');
	this.displayTime = new Date();//更新窗口的显示时间，便于延迟加载
	//var alarmContent = alarm.alarmTypeDescr;//tpl.applyTemplate(alarm);
//notifyWindow.update(alarmContent); //根据数据，填充模板，再更新到窗口上
    //console.log(alarmContent);
    //notifyWindow.show();
	//if(alarm)
	  //this.updateNotify(alarm);

}

//清除报警窗口中的内容
AlarmGrid.clearWindow = function()
{
	$("#alarmBox").empty();
	this.popNum = 0;
}

	

